import time
import sys
import json
import io

# 设置sys.stdout的编码为UTF-8
sys.stdout.reconfigure(encoding='utf-8')
# 创建一个新的输出流，并设置编码为UTF-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
# 将 当且路径 添加到path中
sys.path.append(r'.')


from DrissionPage import ChromiumPage
from DrissionPage._pages.chromium_tab import ChromiumTab


def load_param_datas_from_file(file_path):
    with open(file_path, "r", encoding='utf-8') as f:
        json_file_datas = json.load(f)  # load的传入参数为字符串类型
        return json_file_datas




def open_url():
    json_data = load_param_datas_from_file('open_url.data.json')
    port = json_data['port']
    openUrl = json_data['openUrl']

    page = ChromiumPage(addr_or_opts='127.0.0.1:' + str(port))
    tab = page.new_tab()  # 新建标签页，获取标签页对象
    tab.get(openUrl)
    # 等待页面加载
    tab.wait.load_start()
    closeTable = json_data['closeTable']

    if closeTable:
        tab.close()


open_url()